// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from "axios"

//默认头像地址
Vue.prototype.default_avatar = 'http://vue.api.sidlee.xyz/imgs/default_avatar.jpg';
//cdn
Vue.prototype.cdn = 'http://vue.api.sidlee.xyz/';

//axios配置
//axios.defaults.baseURL = '/api'
//axios拦截器
axios.interceptors.request.use(config=>{
  let token = localStorage.getItem('token');
  if(token){
    config.headers['Authorization'] = token
  }

  return config;
})
// axios.interceptors.response.use(config=>{
//   console.log(config,'ss')
//   return config
// })
Vue.config.productionTip = false

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
Vue.prototype.axios = axios

//路由守卫，处理404
router.beforeEach((to,form,next)=>{
  let token = localStorage.getItem('token')

  if (token){
    //如果有token ，放行
    next()
  }
  else{
    //没有 token
    if(to.path == '/login'){
      //如果访问的是login，则放行
      next()
    }
    else{
      //没有token，访问的也不是login，则强制跳转到login页面
      next('/login')
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
